<template>
  <div class="video_box">
    <navigate>心理视频</navigate>
    <div class="tab_bar">
      <div v-for="(item, index) in tabBar" :key="index" class="item_bar"
        :class="currentBar == index ? 'active_bar' : ''" @click="chooseBar(item.id, index)">
        <div class="item_txt">{{ item.name }}</div>
        <div class="item_line" v-if="currentBar == index"></div>
      </div>
    </div>

    <div class="video_content">
      <div class="video_num">该分类下共有视频:{{ videoTotal }}个</div>
      <div v-for="(item, index) in videoList" :key="index" class="item_content" @click="jumpPage(item)">
        <div class="content_tit">{{ item.name }}</div>

        <div class="content_video">
          <video :src="item.file" :ref="(el) => {
            if (el) videoRefs[index] = el;
          }
            " @play="onPlay(index)" @pause="onPause(index)" :poster="item.img"></video>
          <img src="@/assets/images/function/transparent_pause_icon.png" alt="" class="video_img" v-if="!item.isPlaying"
            @click.stop="toPlay(1, index)" />
          <img src="@/assets/images/function/transparent_play_icon.png" alt="" class="video_img" v-if="item.isPlaying"
            @click.stop="toPlay(2, index)" />
        </div>
        <div class="content_label">
          标签：<span>{{ item.label ? item.label.join("、 ") : "" }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { joinUrlImg } from "@/utils/setUrlImg";
import {
  queryVideoType,
  queryVideoList,
} from "@/api/funcation/psychologicalVideo";
export default {
  data() {
    return {
      isShowPlayImg: true,
      currentBar: 0,
      tabBar: [],
      pageSize: 10,
      page: 1,
      videoList: [],
      videoTotal: 0,
      videoRefs: [],
    };
  },
  created() {
    this.getVideoType();
  },
  methods: {
    joinUrlImg,
    // 播放视频
    toPlay(type, index) {
      const videoPlayer = this.videoRefs[index];
      if (!videoPlayer) return;

      if (type === 1) {
        // 播放
        videoPlayer.play();
        this.$set(this.videoList[index], "isPlaying", true);
      } else {
        // 暂停
        videoPlayer.pause();
        this.$set(this.videoList[index], "isPlaying", false);
      }
    },
    // 视频播放事件
    onPlay(index) {
      this.$set(this.videoList[index], "isPlaying", true);
    },
    // 视频暂停事件
    onPause(index) {
      this.$set(this.videoList[index], "isPlaying", false);
    },
    async getVideoType() {
      queryVideoType;
      let res = await queryVideoType();
      this.tabBar = res.data;
      this.getVideoList(this.tabBar[0].id);
    },
    async getVideoList(id) {
      let par = {
        pageSize: this.pageSize,
        page: this.page,
        type_id: id,
      };
      let res = await queryVideoList(par);
      this.videoList = res.data.data.map((item) => ({
        ...item,
        isPlaying: false,
      }));
      this.videoTotal = res.data.total;
      this.videoRefs = [];
    },

    chooseBar(id, index) {
      this.currentBar = index;
      this.getVideoList(id);
    },
    jumpPage(item) {
      this.$router.push(`/video?videoInfo=${JSON.stringify(item)}`);
    },
  },
};
</script>

<style lang="less" scoped>
.video_box {
  background-color: #f4f6fa;
}

.tab_bar {
  display: flex;
  align-items: center;
  overflow: hidden;
  overflow-x: scroll;
  color: #333333;
  font-size: 14px;
  padding: 0 20px;
  width: 100%;
  height: 54.33px;
  white-space: nowrap;

  &::-webkit-scrollbar {
    display: none;
  }
}

.item_bar {
  margin-right: 39px;
  font-size: 14px;
  font-weight: normal;
  line-height: 1;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  padding-bottom: 10px;
}

.active_bar {
  font-weight: bold;
  color: #00aeff;
}

.item_line {
  position: absolute;
  bottom: -8.33px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  width: 13.33px;
  height: 5.33px;
  background: linear-gradient(-10deg,
      rgba(255, 255, 255, 0.3) 0%,
      rgba(255, 255, 255, 0) 100%),
    #00aeff;
  border-radius: 2.67px;
  opacity: 0;
  transition: opacity 0.3s;
}

.active_bar .item_line {
  opacity: 1;
}

.item_bar:last-child {
  margin-right: 0;
}

//
.video_content {
  padding: 0 10px;
  color: #333333;
}

.item_content {
  padding: 16px 10px 10px 13.33px;
  background-color: #fff;
  border-radius: 6.67px;
  margin-bottom: 6.67px;
}

.content_tit {
  padding-left: 10.33px;
  font-size: 15.33px;
}

.content_video {
  position: relative;
  width: 100%;
  height: 200px;
  margin: 13px 0;

  video {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .video_img {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50px;
    height: 50px;
    cursor: pointer;
  }
}

.video_num {
  margin: 10px 0;
  height: 12px;
  opacity: 0.5;
  font-size: 12px;
}

.content_label {
  font-size: 12px;
  opacity: 0.5;
}
</style>
